package com.zlp.controller;
import com.zlp.utils.JwtTokenUtil;
import com.zlp.utils.api.Result;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.sql.Blob;

/**
 * @author LiPing.Zou
 * @since 1.0.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private JwtTokenUtil jwtTokenUtil;

	@Value("${jwt.tokenHeader}")
	private String tokenHeader;

	@Value("${jwt.tokenBearer}")
	private String tokenBearer;


	/**
	 * 获取用户信息
	 * @param authentication
	 * @param httpServletRequest HttpServletRequest
	 * @date: 2021/3/29 17:15
	 * @return: Principal
	 */
	@GetMapping("/getCurrentUser")
	public Result<Object> getCurrentUser(HttpServletRequest httpServletRequest,
										 Authentication authentication) {

		String header = httpServletRequest.getHeader(tokenHeader);
		String token = header.substring(tokenBearer.length());

		Claims claims = jwtTokenUtil.getClaimsFromToken(token);
		return Result.success(claims);
	}

}